﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using ProjTemp.Desktop.Common.Events;
using Microsoft.Practices.Prism.Events;
using ProjTemp.Desktop.Common.Controller;
using Mysoft.ServerMonitoring.OMKit.Infrastructure;
using ProjTemp.Common.Logging;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.ServiceLocation;
using ProjTemp.Common;
using ProjTemp.Desktop.Common;
using ProjTemp.Desktop.Common.MessageBox;
using ProjTemp.Desktop.Common.Container;
using ProjTemp.Desktop.Common.View;

namespace Mysoft.ServerMonitoring.OMKit.Controller
{
    [Export(MefContractNames.ApplicationControllerContractName, typeof(IController))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class ApplicationController : ControllerBase
    {
        [ImportingConstructor]
        public ApplicationController(ILogger logger, IRegionManager regionManager, IEventAggregator eventAggregator, IServiceLocator serviceLocator)
            : base(logger, regionManager, eventAggregator, serviceLocator)
        {
            this._logger.LogType = typeof(ApplicationController);

            this._logger.Debug(GlobalConst.ObjectConstructed);
        }

        public override void Run()
        {
            this._eventAggregator.GetEvent<NavigationRequestEvent>().Subscribe(RequestNavigation, ThreadOption.UIThread);
        }

        private void RequestNavigation(NavigationRequest navigationRequest)
        {
            if (navigationRequest == null)
                throw new ArgumentNullException("navigationRequest");

            _logger.DebugFormat("NavigationType: {0}, ContainerName: {1}, Topic: {2}", navigationRequest.NavigationType, navigationRequest.ContainerName, navigationRequest.Topic);

            switch (navigationRequest.NavigationType)
            {
                case NavigationType.Region:
                    {
                        this._regionManager.RequestNavigate(navigationRequest.ContainerName, navigationRequest.Topic);
                    }
                    break;
                case NavigationType.ModalWindow:
                case NavigationType.NonModalWindow:
                    {
                        var view = this._serviceLocator.GetInstance<object>(navigationRequest.Topic) as VBase;
                        if (view == null)
                        {
                            this._logger.ErrorFormat(DesktopConst.CannotNavigateToTargetView, navigationRequest.Topic);
                            return;
                        }

                        var container = new WindowWrapper() as IContainer;
                        container.Title = view.Header;
                        container.Content = view;
                        view.Container = container;
                        if(navigationRequest.Owner != null)
                            container.Owner = navigationRequest.Owner;
                        if (navigationRequest.Callback != null)
                        {
                            container.Closed += new EventHandler((sender, e) => {
                                navigationRequest.Callback(container.ReturnValue);
                            });                            
                        }

                        if (navigationRequest.NavigationType == NavigationType.ModalWindow)
                            container.Show(navigationRequest.ContainerSetting);
                        else
                            container.ShowNonModal(navigationRequest.ContainerSetting);
                    }
                    break;
                case NavigationType.PopupLayer:
                    {
                        var view = this._serviceLocator.GetInstance<object>(navigationRequest.Topic) as VBase;
                        if (view == null)
                        {
                            this._logger.ErrorFormat(DesktopConst.CannotNavigateToTargetView, navigationRequest.Topic);
                            return;
                        }
                        var container = navigationRequest.Owner as IContainer;
                        if (container == null)
                        {
                            this._logger.ErrorFormat(DesktopConst.CannotResolvePopupLayerContainer);
                            return;
                        }

                        container.Title = view.Header;
                        container.Content = view;
                        view.Container = container;
                        if (navigationRequest.Owner != null)
                            container.Owner = navigationRequest.Owner;
                        if (navigationRequest.Callback != null)
                        {
                            container.Closed += new EventHandler((sender, e) =>
                            {
                                navigationRequest.Callback(container.ReturnValue);
                            });
                        }

                        container.Show(navigationRequest.ContainerSetting);
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
